<template>
  <div style="height: calc((100% - 70px) * 0.5); margin-top: 15px" v-show="patTypeId">
    <el-table v-loading="listLoading" :data="list" size="small" height="100%" style="min-height: 200px" v-if="patTypeId">
      <el-table-column label="序号" type="index" width="50" />
      <el-table-column label="标准内容" prop="content" show-overflow-tooltip min-width="200" />
      <el-table-column label="是否启用" align="center" width="80">
        <el-tag slot-scope="{ row }" size="small" :type="row.isEnable ? 'primary' : 'info'">{{ row.isEnable ? '启用' : '启用' }}</el-tag>
      </el-table-column>
      <el-table-column align="center" width="120" fixed="right">
        <template slot="header">
          <el-button size="mini" type="text" icon="el-icon-plus" @click="$refs.EditItem.open({ patTypeId })" v-if="showBtns.add">新增</el-button>
        </template>
        <template slot-scope="{ row }">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="$refs.EditItem.open(row)" v-if="showBtns.edit">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" :loading="row.removeLoading" @click="btnRemoveItem(row)" v-if="showBtns.remove">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <EditItem ref="EditItem" v-model="showEditItem" @ok="getList" />
  </div>
</template>
<script>
// 检查标准列表
import auth from '@/plugins/auth'
import { debounce } from '@/utils'
import { listPatStandard, delPatStandard } from '@/api/cq/patrol'
import EditItem from './EditPatStandard'
export default {
  name: 'PatStandardList',
  components: { EditItem },
  data() {
    return {
      patTypeId: '',
      listLoading: false,
      list: [],
      showEditItem: false
    }
  },
  computed: {
    showBtns() {
      // 操作按钮权限
      const permCode = 'cq:patrol:standard'
      const edit = auth.hasPermi(`${permCode}:edit`)
      const add = auth.hasPermi(`${permCode}:add`)
      const remove = auth.hasPermi(`${permCode}:remove`)
      return { edit, add, remove }
    }
  },
  created() {
    this.btnRemoveItem = debounce(this.removeItem, 1e3, true)
  },
  methods: {
    reset() {
      this.patTypeId = ''
      this.list = []
    },
    open(item = {}) {
      if (!item.patTypeId) return this.reset()
      this.patTypeId = item.patTypeId
      this.getList()
    },
    async getList() {
      const { patTypeId } = this
      if (!patTypeId) return this.reset()
      this.listLoading = true
      const res = await listPatStandard({ patTypeId }).catch(() => false)
      this.listLoading = false
      if (res === false) return

      const data = res.data || []
      data.map(i => {
        i.removeLoading = false
      })
      this.list = data
    },
    async removeItem(item) {
      if (item.removeLoading) return
      const content = item.content.slice(0, 100) + (item.content.length > 100 ? '...' : '')
      const res1 = await this.$confirm(`确认删除：${content}`, '警告', { type: 'warning' }).catch(() => false)
      if (res1 !== 'confirm') return

      item.removeLoading = true
      const res = await delPatStandard(item.patStandardId).catch(() => false)
      item.removeLoading = false
      if (res === false) return

      this.msgSuccess('删除成功')
      this.getList()
    }
  }
}
</script>
